﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PipeAuto.DAL;
using PipeAuto.DAL.PipeAutoDataSetTableAdapters;
using System.Security.Principal;
using WinformAuthentication;
using System.Threading;

namespace PipeAuto.MgrPlatform
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (uNameTextBox.Text.Trim()==string.Empty)
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }

            if (pwdTextBox.Text==string.Empty)
            {
                MessageBox.Show("密码不能为空！");
                return;
            }

            try
            {
                UsersTableAdapter myAdapter = new UsersTableAdapter();

                string myHashedPwd = MgrPlatformGlobal.ComputerMD5(pwdTextBox.Text);

                var myTable = myAdapter.GetDataByIdentity(uNameTextBox.Text, myHashedPwd);

                if (myTable.Count==0)
                {
                    MessageBox.Show("用户名或者密码错误，请检查！");
                    return;
                }


                MemberShipTableAdapter myMemberAdapter = new MemberShipTableAdapter();



                var myRow=myTable[0];


                PipeAutoDataSet.MemberShipDataTable myMemberTable=null;

                myMemberTable = myMemberAdapter.GetDataByRoleType(2,myRow.RoleID);
                

                //is super administrator
                bool isSuperAdmin=myTable[0].UName=="admin";

                if (myMemberTable.Count==0&&!isSuperAdmin)
                {
                    MessageBox.Show("您没有权限登入该平台！");
                    return;
                }

                string[] myMember = null;

                //sa has all app
                if (isSuperAdmin == true)
                {
                    AppDicTableAdapter myAppAdapter = new AppDicTableAdapter();

                    var myAppTable = myAppAdapter.GetData();

                    myMember = new string[myAppTable.Count];

                    for (int j = 0; j < myAppTable.Count; j++)
                    {
                        myMember[j] = myAppTable[j].ID;
                    }
                }
                else
                {

                    myMember = new string[myMemberTable.Count];
                    for (int i = 0; i < myMemberTable.Count; i++)
                    {
                        myMember[i] = myMemberTable[i].AppID;
                    }
                }

                WinformIdentity myIdentity = new WinformIdentity { ID = myRow.ID, UserName = myRow.UName, Name = myRow.Name, PassWord = myRow.PWD };
                GenericPrincipal myPrincipal = new GenericPrincipal(myIdentity, myMember);
                Thread.CurrentPrincipal = myPrincipal;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void uNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==(char)Keys.Enter)
            {
                pwdTextBox.Focus();
                e.Handled=true;
            }
        }

        private void pwdTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==(char)Keys.Enter)
            {
                okButton_Click(sender, null);
                e.Handled = true;
            }
        }
    }
}
